#ifndef CRIGHTVIEW_HPP
#define CRIGHTVIEW_HPP

#include <time.h>
#include <shlobj.h>
#include <shtypes.h>
#include <windows.h>
#include <strsafe.h>
#include <commctrl.h>

class CRightView
{
public:
    CRightView() {}
    ~CRightView() {}
    CRightView(LPCTSTR lpszClassName, HINSTANCE hInstance)
    {
        m_hWnd = NULL;
        m_hParent = NULL;
        m_wcex = {0};
        m_hFont = NULL;
        m_rcMenuBar = {0};
        m_rcToolBar = {0};
        Register(lpszClassName, hInstance);
    }
    CRightView(const CRightView &) = delete;
    CRightView &operator=(const CRightView &) = delete;
    HWND GetHWND() const
    {
        return m_hWnd;
    }
    void SetToolBarRect(RECT rcToolBar)
    {
        m_rcToolBar = rcToolBar;
    }
    void SetMenuBarRect(RECT rcMenuBar)
    {
        m_rcMenuBar = rcMenuBar;
    }
    HFONT GetFont(TCHAR *lpFont, UINT uSize = 20)
    {
        if (m_hFont)
        {
            return m_hFont;
        }
        else
        {
            LOGFONT logfont;
            ZeroMemory(&logfont, sizeof(LOGFONT));
            logfont.lfHeight = uSize;
            if (lpFont)
            {
                StringCchCopy(logfont.lfFaceName, 255, lpFont);
            }
            else
            {
                StringCchCopy(logfont.lfFaceName, 255, TEXT("Microsoft YaHei"));
            }
            m_hFont = CreateFontIndirect(&logfont);
            return m_hFont;
        }
    }
    LRESULT Register(LPCTSTR lpszClassName, HINSTANCE hInstance)
    {
        m_wcex = {0};
        m_wcex.cbSize = sizeof(WNDCLASSEX);
        m_wcex.cbClsExtra = 0;                                      // 类的额外内存
        m_wcex.cbWndExtra = 0;                                      // 窗口额外内存
        m_wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); // 背景
        m_wcex.hCursor = LoadCursor(NULL, IDC_ARROW);               // 光标 第一个为NULL代表使用系统提供的光标
        m_wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);             // 图标 第一个为NULL代表使用系统提供的图标
        m_wcex.hInstance = hInstance;                               // 应用程序实例句柄
        m_wcex.lpfnWndProc = CRightView::WndProc;                   // 回调函数
        m_wcex.lpszClassName = lpszClassName;                       // 窗口类名称
        m_wcex.lpszMenuName = NULL;                                 // 菜单名称
        m_wcex.style = CS_HREDRAW | CS_VREDRAW;                     // 显示风格
        m_hInstance = hInstance;
        if (!RegisterClassEx(&m_wcex))
        {
            MessageBox(NULL, TEXT("Failed to register child window class"), TEXT("Error"), MB_OK | MB_ICONERROR);
            return -1;
        }
        return 0;
    }
    LRESULT Create(HWND hParent, LPRECT lpRect, LPCTSTR szWindowName)
    {
        // Create the child window
        RECT rect;
        if (!lpRect)
        {
            GetClientRect(hParent, &rect);
            lpRect = &rect;
        }
        m_nMenuBarHeight = m_rcMenuBar.bottom - m_rcMenuBar.top;
        m_nToolBarHeight = m_rcToolBar.bottom - m_rcToolBar.top;
        lpRect->top += m_nToolBarHeight;
        m_hWnd = CreateWindow(m_wcex.lpszClassName, szWindowName, WS_CHILD | WS_VISIBLE,
                              lpRect->left, lpRect->top, lpRect->right - lpRect->left, lpRect->bottom - lpRect->top,
                              hParent, NULL, m_hInstance, this);
        m_hParent = hParent;
        SetWindowLongPtr(m_hWnd, GWLP_USERDATA, (LONG_PTR)this);

        CreateRightCtrl();
        return 0;
    }
    void CreateRightCtrl()
    {
        RECT rect;
        GetClientRect(m_hParent, &rect);
        rect.left = rect.left + nWindowEdge;
        rect.top = rect.top + nWindowEdge;
        rect.right = rect.right - rect.left - nWindowEdge;
        rect.bottom = rect.bottom - rect.top - m_nToolBarHeight - nWindowEdge;
        m_hListViewVar = CreateWindow(WC_LISTVIEW, TEXT(""), WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_REPORT | LVS_SHOWSELALWAYS, rect.left, rect.top, rect.right, rect.bottom, m_hWnd, (HMENU)5, NULL, NULL);
        ListView_SetExtendedListViewStyle(m_hListViewVar, LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP | LVS_EX_GRIDLINES);
        SendMessage(m_hListViewVar, WM_SETFONT, (WPARAM)GetFont(NULL), TRUE);

        LVCOLUMN colTime;
        colTime.mask |= LVCF_WIDTH | LVCF_TEXT;
        colTime.cx = 100;
        colTime.pszText = TEXT("time");
        ListView_InsertColumn(m_hListViewVar, 0, &colTime);

        LVCOLUMN colContent;
        colContent.mask |= LVCF_WIDTH | LVCF_TEXT;
        colContent.cx = 200;
        colContent.pszText = TEXT("content");
        ListView_InsertColumn(m_hListViewVar, 1, &colContent);

        LVITEM rowInfo = {0};
        for (int i = 0; i < 10; ++i)
        {
            ZeroMemory(&rowInfo, sizeof(rowInfo));
            rowInfo.iItem = i;
            ListView_InsertItem(m_hListViewVar, &rowInfo);

            TCHAR szBuffer[255];
            StringCchPrintf(szBuffer, 255, TEXT("element %d"), i);
            ListView_SetItemText(m_hListViewVar, i, 0, szBuffer);
            ListView_SetItemText(m_hListViewVar, i, 1, szBuffer);
        }
    }
    BOOL appendElement(TCHAR *szBuffer)
    {
        LVITEM rowInfo = {0};
        int nRows = SendMessage(m_hListViewVar, LVM_GETITEMCOUNT, 0, 0);

        ZeroMemory(&rowInfo, sizeof(rowInfo));

        SYSTEMTIME st;
        GetLocalTime(&st);
        TCHAR szTimeBuffer[255];
        StringCchPrintf(szTimeBuffer, 255, TEXT("%d-%d-%d %d:%d:%d"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);

        rowInfo.mask |= LVIF_TEXT;
        rowInfo.iItem = nRows;
        rowInfo.iSubItem = 0;
        rowInfo.pszText = szTimeBuffer;
        SendMessage(m_hListViewVar, LVM_INSERTITEM, 0, (LPARAM)&rowInfo);

        ZeroMemory(&rowInfo, sizeof(rowInfo));
        rowInfo.mask |= LVIF_TEXT;
        rowInfo.iItem = nRows;
        rowInfo.iSubItem = 1;
        rowInfo.pszText = szBuffer;
        SendMessage(m_hListViewVar, LVM_SETITEM, 0, (LPARAM)&rowInfo);
        return TRUE;
    }
    static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        CRightView *pThis = (CRightView *)GetWindowLongPtr(hWnd, GWLP_USERDATA);
        if (pThis)
        {
            return pThis->WindowProc(hWnd, uMsg, wParam, lParam);
        }
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
    LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg)
        {
        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            HPEN hPen = CreatePen(PS_NULL, 1, RGB(0, 0, 0));
            HPEN holdPen = (HPEN)SelectObject(hdc, hPen);
            HBRUSH hBrush = CreateSolidBrush(RGB(200, 255, 200));
            HBRUSH holdBrush = (HBRUSH)SelectObject(hdc, hBrush);

            RECT rect;
            GetClientRect(hWnd, &rect);
            // Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);

            SelectObject(hdc, holdPen);
            SelectObject(hdc, holdBrush);
            DeleteObject(hPen);
            DeleteObject(hBrush);
            ReleaseDC(hWnd, hdc);
            EndPaint(hWnd, &ps);
            break;
        }
        case WM_COMMAND:
        {
            TCHAR szTitle[MAX_PATH] = {0};
            StringCchPrintf(szTitle, 256, TEXT("我收到了: %d"), LOWORD(wParam));
            // MessageBox(0, szTitle, TEXT("我收到了"), MB_OK);
            break;
        }
        case WM_SIZE:
        {
            RECT rcParent;
            GetClientRect(m_hParent, &rcParent);
            rcParent.top += m_rcToolBar.bottom - m_rcToolBar.top;
            MoveWindow(m_hWnd, rcParent.left, rcParent.top, rcParent.right, rcParent.bottom, TRUE);

            RECT rect;
            GetClientRect(m_hParent, &rect);
            rect.left = rect.left + nWindowEdge;
            rect.top = rect.top + nWindowEdge;
            rect.right = rect.right - rect.left - nWindowEdge;
            rect.bottom = rect.bottom - rect.top - m_nToolBarHeight - nWindowEdge;
            MoveWindow(m_hListViewVar, rect.left, rect.top, rect.right, rect.bottom, TRUE);

            InvalidateRect(hWnd, NULL, FALSE);
            break;
        }
        case WM_MOUSEMOVE:
        {
            int xPos = LOWORD(lParam);
            int yPos = HIWORD(lParam);
            TCHAR buffer[256];
            StringCchPrintf(buffer, 256, TEXT("xPos: %d, yPos: %d"), xPos, yPos);
            // SetWindowText(m_hParent, buffer);
            break;
        }
        case WM_CLOSE:
        {
            DestroyWindow(hWnd);
            break;
        }
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            break;
        }
        default:
            break;
        }
        return DefWindowProc(m_hWnd, uMsg, wParam, lParam);
    }

public:
    UINT nWindowEdge = 5;

    HWND m_hListViewVar;

    UINT m_nMenuBarHeight;
    UINT m_nToolBarHeight;
    HWND m_hWnd;
    HWND m_hParent;
    HFONT m_hFont;
    WNDCLASSEX m_wcex;
    RECT m_rcMenuBar;
    RECT m_rcToolBar;
    HINSTANCE m_hInstance;
};

#endif